package cn.cj.erp.common.utils.encrypt;

import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import java.nio.charset.StandardCharsets;

/**
 * 国密sm4算法工具类，用于加密和解密数据
 *
 * @author chenjian
 **/
public class Sm4Util {
    /** 国米密钥，记住，要16字节 */
    private static final String INNER_SM4_KEY = "12dfe34ffs444hy@";

    public static String encrypt(String plainText) {
        SymmetricCrypto sm4 = SmUtil.sm4(INNER_SM4_KEY.getBytes(StandardCharsets.UTF_8));
        return sm4.encryptBase64(plainText);
    }

    public static String decrypt(String encryptData) {
        SymmetricCrypto sm4 = SmUtil.sm4(INNER_SM4_KEY.getBytes(StandardCharsets.UTF_8));
        return sm4.decryptStr(Base64Util.decode(encryptData), StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        String encrypt =  encrypt("陈建");
        System.out.printf("encrypt 耗时：%d,  %s\n", (System.currentTimeMillis() - start), encrypt);
        start = System.currentTimeMillis();
        String decrypt = decrypt(encrypt);
        System.out.printf("decrypt 耗时：%d,  %s\n", (System.currentTimeMillis() - start), decrypt);


        start = System.currentTimeMillis();
        encrypt =  encrypt("陈建131311");
        System.out.printf("encrypt 耗时：%d,  %s\n", (System.currentTimeMillis() - start), encrypt);
        start = System.currentTimeMillis();
        decrypt = decrypt(encrypt);
        System.out.printf("decrypt 耗时：%d,  %s\n", (System.currentTimeMillis() - start), decrypt);

        start = System.currentTimeMillis();
        encrypt =  encrypt("陈建1213131");
        System.out.printf("encrypt 耗时：%d,  %s\n", (System.currentTimeMillis() - start), encrypt);
        start = System.currentTimeMillis();
        decrypt = decrypt(encrypt);
        System.out.printf("decrypt 耗时：%d,  %s\n", (System.currentTimeMillis() - start), decrypt);
    }

}
